<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/10/16
 * Time: 17:34
 */
 
namespace Modules\Admin\Service;

use HiCommon\Repository\DisclaimerRepository;

class DisclaimerService
{

    /**
     * 获取所有权限
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all(){
        try {
            $arr_option = ['order'=>['key'=>'id','value'=>'asc']];
            $data = DisclaimerRepository::get_all($arr_option)->toarray();
            return  hello_success('成功',$data);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list()
    {
        try {

            $arr_option = ['order'=>['key'=>'id','value'=>'desc']];
            $data = DisclaimerRepository::get_list($arr_option);
            $data = list_page_data($data);
            return  hello_success('成功',$data);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 添加一条数据
     *
     * @param $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            //添加
            $res = DisclaimerRepository::add_one($arr_input)->toarray();
            if(empty($res)){
                return hello_error('添加失败');
            }

            return  hello_success('成功',$res);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id)
    {
        $n_id = (int) $n_id;
        if($n_id <= 0) {
            hello_error('数据ID不能为空');
        }

        //查询
        $res = DisclaimerRepository::get_one($n_id)->toarray();
        if(empty($res)){
            return hello_error('查询失败');
        }

        return  hello_success('查询成功',$res);

    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = DisclaimerRepository::edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 修改单个字段
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state($arr_input){
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = DisclaimerRepository::edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = DisclaimerRepository::delete_one($n_id);
            if($res === false){
                return hello_error('操作失败');
            }
            return  hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }
}